﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class QL_Chitiet_monhoc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["quyen"] != null)
            {
                if (Session["quyen"] != null)
                {
                    if ((Session["quyen"].ToString() == "AD") || (Session["quyen"].ToString() == "GV"))
                    {

                        if (Session["Tendangnhap"] != null && Session["Tendangnhap"].ToString() != "")
                        {

                            MasterPage mt = this.Master as MasterPage;
                            LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                            lb.Visible = false;
                            Label bl = mt.FindControl("lblDnthanhcong") as Label;
                            bl.Visible = true;
                            Label blten = mt.FindControl("lbtendn") as Label;
                            blten.Visible = true;
                            blten.Text = Session["Tendangnhap"].ToString();
                            LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                            lbDangky.Visible = false;
                            LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                            lbthoat.Visible = true;

                        }
                        if (!IsPostBack)
                        {
                            napMonhoc();
                            Napmonhoc1();
                            Napgiaovien1();
                            Napmonhoc2();
                            Napgiaovien2();
                        }
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }
        }
        private void Napmonhoc1()
        {
            try
            {
                drMa.DataSource = new MonhocBL().SelectAllDistiny();
                drMa.DataTextField = "Tenmon";
                drMa.DataValueField = "Mamonhoc";
                drMa.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        private void Napmonhoc2()
        {
            try
            {
                drMa2.DataSource = new MonhocBL().SelectAllDistiny();
                drMa2.DataTextField = "Tenmon";
                drMa2.DataValueField = "Mamonhoc";
                drMa2.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        private void Napgiaovien1()
        {
            try
            {
                drGv.DataSource = new ThanhvienBL().GetGV();
                drGv.DataTextField = "Hovaten";
                drGv.DataValueField = "Tentaikhoan";
                drGv.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        private void Napgiaovien2()
        {
            try
            {
                drGv2.DataSource = new ThanhvienBL().GetGV(); ;
                drGv2.DataTextField = "Hovaten";
                drGv2.DataValueField = "Tentaikhoan";
                drGv2.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        private void napMonhoc()
        {
            try
            {
                griMonhoc.DataSource = new CT_MonhocBL().SelectAll();
                griMonhoc.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        private void Lay_mon(int id)//thuc hien chuc nang bind data 
        {
            try
            {
                CT_MonhocDO obj = new CT_MonhocBL().Select(new CT_MonhocDO { ID = id });
                drMa.SelectedValue = obj.Mamonhoc;
                drGv.SelectedValue = obj.Tentaikhoan;
            }

            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
                
            }
        }

        protected void griMonhoc_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            int result = 0;
            try
            {
                if (e.CommandName == "xoa")
                {
                    //result = mBAL.Xoa_mon_chitiet(ma);
                    result = new CT_MonhocBL().Delete(new CT_MonhocDO { ID = id });

                    if (result > 0)
                    {
                        lblLoi.Text = "Xoa thanh cong !";
                        napMonhoc();
                    }
                    else
                    {
                        lblLoi.Text = "Xoa khong thang cong !";
                    }
                }
                if (e.CommandName == "sua")
                {
                    txtmamon.Text = Convert.ToString(id);
                    pnSua.Visible = true;
                    Lay_mon(id);
                    pnThem.Visible = false;
                }
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
                
            }
        }
        protected void imgtk_Click(object sender, ImageClickEventArgs e)
        {
            if (txtTimkiem.Text == "")
            {
                lblLoi.Text = "Bạn chưa nhập thông tin tìm kiếm";
            }
            else
            {
                string ma = txtTimkiem.Text;
                if (rd_Ykien.SelectedItem.Value == "0")
                {

                    Tim_kiem(0, ma);
                }
                if (rd_Ykien.SelectedItem.Value == "1")
                {
                    Tim_kiem(1, ma);
                }
            }
        }

        protected void imgRe_Click(object sender, ImageClickEventArgs e)
        {
            napMonhoc();
            pnThem.Visible = false;
            pnSua.Visible = false;
            lblLoi.Visible = false;
        }
        protected void btDongy_Click(object sender, EventArgs e)
        {
            CT_MonhocDO obj = new CT_MonhocDO();
            int kq = 0;
            int id = int.Parse(txtmamon.Text.ToString());
            //int id1 = txtmamon.Text.ToString();
            obj.ID = id;
            obj.Mamonhoc = drMa.SelectedValue;
            obj.Tentaikhoan = drGv.SelectedValue;
            try
            {
                kq = new CT_MonhocBL().Update(obj);
                if (kq > 0)
                {
                    lblLoi.Text = "Update khong thanh cong !";
                    
                }
                else
                {
                    lblLoi.Text = "Update thanh cong !";
                    napMonhoc();
                    pnSua.Visible = false;
                }
            }
            catch (Exception ex)
            {
                lblLoi.Text = ex.Message;
            }
            finally
            {
                
            }
        }
        private void Tim_kiem(int trangthai,string tukhoa)
        {
            try
            {
                griMonhoc.DataSource = new CT_MonhocBL().Timkiem(tukhoa);
                griMonhoc.DataBind();
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
                
            }
        }
        protected void btLamlai_Click(object sender, EventArgs e)
        {
            pnSua.Visible = false;
        }
        protected void imgThem_Click(object sender, ImageClickEventArgs e)
        {

            //NapGV();
            pnThem.Visible = true;
            pnSua.Visible = false;

        }

        protected void btLuu_Click(object sender, EventArgs e)
        {
            CT_MonhocDO obj = new CT_MonhocDO();
            int kq = 0;
            obj.Mamonhoc = drMa2.SelectedValue;
            obj.Tentaikhoan = drGv2.SelectedValue;
            try
            {
                kq = new CT_MonhocBL().Insert(obj);
                if (kq > 0)
                {
                    lblLoi.Text = "Them thanh cong !";
                    //kq = mBAL.Them_gmon_gv(mamon, magv);
                    napMonhoc();
                    //pnYkien.Visible = false;

                }
                else
                {
                    lblLoi.Text = "Them khong thanh cong !";
                }
            }
            catch (Exception ex)
            {
                lblLoi.Text = ex.Message;
            }
            finally
            {
                
            }

        }

        protected void btHuy_Click(object sender, EventArgs e)
        {
            pnThem.Visible = false;
        }

        protected void griMonhoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griMonhoc.PageIndex = e.NewPageIndex;
            this.napMonhoc();
        }
    }
}
